package message;

import bean.UserInfo;
import com.alibaba.fastjson.JSONObject;
import enums.MessageTypeEnum;

import java.nio.charset.StandardCharsets;

/**
 * @author wb_zhulinzhong
 * @date 2021-04-15 16:21:27
 */
public class LoginMessage extends Message{

    private boolean login = false;

    public LoginMessage(){
        super();
    }

    public LoginMessage(UserInfo userInfo){
        super(userInfo.getUId(), null, JSONObject.toJSONString(userInfo).getBytes(StandardCharsets.UTF_8));
    }

    @Override
    protected void setMediaType() {
        type = MessageTypeEnum.LOGIN.getCode();
    }

    /**
     * 获取用户信息
     * @return
     */
    public UserInfo getUserInfo(){
        String json = new String(getData(), StandardCharsets.UTF_8);
        JSONObject jsonObject = JSONObject.parseObject(json);
        return jsonObject.toJavaObject(UserInfo.class);
    }

    public boolean isLogin() {
        return login;
    }

    public void setLogin(boolean login) {
        this.login = login;
    }
}
